package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;

public class MenuFilterListAdapter extends BaseChoiceAdapter<MenuTabModel> {
	public MenuFilterListAdapter(Context mContext) {
		super(mContext);
	}

	@Override
	public void convert(ViewHolder h, final MenuTabModel t) {
		h.setText(R.id.tv1, t.tab_name);
		ImageView image = h.getView(R.id.iv1);
		if (hasContained(t)) {
			image.setImageResource(R.drawable.added_item);
		} else {
			image.setImageDrawable(new ColorDrawable(Color.TRANSPARENT));
		}
		h.getConvertView().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// # 首先判断是否存在
				if (hasContained(t)) {
					removeFromChoice(t);
				} else {
					addToChoice(t);
				}
				notifyDataSetChanged();
				// # 不存在 的话就加入
				// # 存在的话就删除
			}
		});
	}

	@Override
	protected boolean isEquals(MenuTabModel t1, MenuTabModel t2) {
		return t1 == null || t2 == null ? false : StringUtils.equals(t1.id,
				t2.id);
	}

	@Override
	public int getLayoutId() {
		return R.layout.menu_list_filter_item;
	}

}
